﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using TmU220PrintHelperV2d0.ControlUi;
using ZhangLogSysV1d0;
/// <summary>
/// PrintTempletSetV2d0,PrintTempletV2d0,PrintTempletRowV2d0,AreaTypeV2d0
/// 三个模板元素组成一个模板行，多个模板行组成一个打印模板，多个打印模板构成一个打印模板集
/// zhangfujian-2018-12
/// </summary>
namespace TmU220PrintHelperV2d0.Entity
{
    /// <summary>
    /// 打印模板集
    /// </summary>
    [XmlRoot("打印模板集")]
    public class PrintTempletSetV2d0
    {
        [XmlArray("模板集"), XmlArrayItem("模板")]
        public List<PrintTempletV2d0> PrintTempletList { get; set; }
        public PrintTempletSetV2d0()
        {
            PrintTempletList = new List<PrintTempletV2d0>();
        }
        public static void SaveToFile(PrintTempletSetV2d0 printtTempletInfoSet, string fileName)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(PrintTempletSetV2d0));
            StreamWriter sW = new StreamWriter(fileName);
            try
            {
                xmlSerializer.Serialize(sW, printtTempletInfoSet);
            }
            finally
            {
                sW.Close();
            }
        }
        public static PrintTempletSetV2d0 ReadFromFile(string fileName)
        {
            PrintTempletSetV2d0 result = null;
            try
            {
                if (File.Exists(fileName))
                {
                    StreamReader sR = new StreamReader(fileName);
                    XmlSerializer xmlS = new XmlSerializer(typeof(PrintTempletSetV2d0));
                    result = (PrintTempletSetV2d0)xmlS.Deserialize(sR);
                    sR.Close();
                }
            }
            catch (Exception ex)
            {
                SimpleLoger.Instance.Error(ex);
            }
            return result;
        }
        /// <summary>
        /// 根据模板集，得到模板信息列表
        /// </summary>
        /// <param name="templetSet"></param>
        /// <returns></returns>
        public static List<rms_print_templet_main> GetRmsPrintTempletMainList(PrintTempletSetV2d0 templetSet)
        {
            List<rms_print_templet_main> mainList = new List<rms_print_templet_main>();
            //templetSet.PrintTempletList.ForEach(x => mainList.Add(new rms_print_templet_main(x.TempletGuid.ToString(), x.TempletName, x.TempletType)));
            templetSet.PrintTempletList.ForEach(x => mainList.Add(GetRmsPrintTempletMain(x)));
            return mainList;
        }
        /// <summary>
        /// 将打印模板转化为打印模板主表记录
        /// </summary>
        /// <param name="printTemplet"></param>
        /// <returns></returns>
        public static rms_print_templet_main GetRmsPrintTempletMain(PrintTempletV2d0 printTemplet)
        {
            rms_print_templet_main templetMain = new rms_print_templet_main(
                printTemplet.TempletUid,
                printTemplet.TempletGuid.ToString(), 
                printTemplet.TempletName, 
                printTemplet.TempletType,
                printTemplet.TempletLanguage
                );
            return templetMain;
        }

        /// <summary>
        /// 根据模板集，得到模板明细信息列表，每个模板元素一条记录
        /// </summary>
        /// <param name="templetSet"></param>
        /// <returns></returns>
        public static List<rms_print_templet_detail> GetRmsPrintTempletDetailList(PrintTempletSetV2d0 templetSet)
        {
            List<rms_print_templet_detail> detailList = new List<rms_print_templet_detail>();
            string templetGuid = "";
            int templetUid = 0;
            int rowIndex =0;
            int rowUid = 0;
            int parentId = 0;
            string rowGuid = "";
            string printMode = "";
            string areaType = "";
            //遍历模板集的模板列表
            foreach (var printTemplet in templetSet.PrintTempletList)
            {
                templetGuid = printTemplet.TempletGuid.ToString();
                templetUid = printTemplet.TempletUid;
                //遍历模板的模板行列表
                foreach (var templetRow in printTemplet.PrintTempletRowList)
                {
                    rowIndex = templetRow.RowIndex;
                    rowGuid = templetRow.RowGuid.ToString();
                    parentId = templetUid;
                    printMode = templetRow.PrintMode;
                    areaType = templetRow.AreaType.ToString();
                    //遍历模板行的模板元素列表
                    foreach ( var rowElement in templetRow.TempletElementList)
                    {
                        rms_print_templet_detail templetDetail = new rms_print_templet_detail();
                        templetDetail.area_type = areaType;
                        templetDetail.content_text = rowElement.ContentText;
                        templetDetail.font_color = rowElement.FontColor;
                        templetDetail.font_name = rowElement.FontName;
                        templetDetail.font_size = rowElement.FontSize;
                        templetDetail.horizontal_align = rowElement.HorizontalAlign;
                        templetDetail.element_index = rowElement.ElementIndex;
                        templetDetail.is_bold = rowElement.IsBold;
                        templetDetail.is_cutoffline = rowElement.IsCutoffLine;
                        templetDetail.is_double_height = rowElement.IsDoubleHeight;
                        templetDetail.is_double_width = rowElement.IsDoubleWidth;
                        templetDetail.is_underline = rowElement.IsUnderLine;
                        templetDetail.print_mode = printMode;
                        templetDetail.row_index = rowIndex;
                        templetDetail.row_guid = rowGuid;
                        templetDetail.parent_id = parentId;
                        templetDetail.templet_guid = templetGuid;
                        templetDetail.vertical_align = rowElement.VerticalAlign;
                        detailList.Add(templetDetail);
                    }
                }
            }
            return detailList;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mainList"></param>
        /// <param name="detailList"></param>
        /// <returns></returns>
        public static PrintTempletSetV2d0 GetPrintTempletSet(
            List<rms_print_templet_main> mainList, List<rms_print_templet_detail> detailList)
        {
            PrintTempletSetV2d0 printTempletSet = new PrintTempletSetV2d0();
            foreach (var templetMain in mainList)
            {
                PrintTempletV2d0 printTemplet = new PrintTempletV2d0();
                printTemplet.TempletUid = templetMain.uid;
                string templetGuid = templetMain.templet_guid;
                printTemplet.TempletGuid = Guid.Parse(templetGuid);
                printTemplet.TempletName = templetMain.templet_name;
                printTemplet.TempletType = templetMain.templet_type;
                printTemplet.TempletLanguage = templetMain.templet_language;
                var subDetailList = detailList.FindAll(x => x.templet_guid == templetGuid);
                List<int> rowIndexList = new List<int>();
                //按照row_index进行分组
                var queryRowIndexGroup = from a in subDetailList
                                         orderby a.row_index
                                         group a by a.row_index;
                foreach (var rowGroup in queryRowIndexGroup)
                {
                    int rowIndex = rowGroup.Key;
                    PrintTempletRowV2d0 templetRow = new PrintTempletRowV2d0();
                    templetRow.ParentId = printTemplet.TempletUid;
                    templetRow.RowIndex = rowIndex;
                    //将字符串转为枚举
                    string areaTypeString = rowGroup.First().area_type;
                    AreaTypeV2d0 areaType = (AreaTypeV2d0)Enum.Parse(typeof(AreaTypeV2d0), areaTypeString);
                    templetRow.AreaType = areaType;
                    templetRow.PrintMode = rowGroup.First().print_mode;
                    templetRow.RowGuid = Guid.Parse(rowGroup.First().row_guid);
                    foreach (var element in rowGroup)
                    {
                        PrintTempletElementV2d0 templetElement = new PrintTempletElementV2d0();
                        templetElement.ContentText = element.content_text;
                        templetElement.ElementIndex = element.element_index;
                        templetElement.FontColor = element.font_color;
                        templetElement.FontName = element.font_name;
                        templetElement.FontSize = element.font_size;
                        templetElement.HorizontalAlign = element.horizontal_align;
                        templetElement.IsBold = element.is_bold.HasValue ? element.is_bold.Value : false;
                        templetElement.IsCutoffLine = element.is_cutoffline.HasValue ? element.is_cutoffline.Value : false;
                        templetElement.IsDoubleHeight = element.is_double_height.HasValue ? element.is_double_height.Value : false;
                        templetElement.IsDoubleWidth = element.is_double_width.HasValue ? element.is_double_width.Value : false;
                        templetElement.IsUnderLine = element.is_underline.HasValue ? element.is_underline.Value : false;
                        templetElement.PrintMode = element.print_mode;
                        templetElement.VerticalAlign = element.vertical_align;
                        templetRow.TempletElementList.Add(templetElement);
                    }
                    printTemplet.PrintTempletRowList.Add(templetRow);
                }
                printTempletSet.PrintTempletList.Add(printTemplet);
            }
            return printTempletSet;
        }
    }
    /// <summary>
    /// 打印模板
    /// </summary>
    [XmlRoot("打印模板")]
    [Serializable]
    public class PrintTempletV2d0
    {
        [XmlElement("模板uid")]
        public int TempletUid { get; set; }
        [XmlElement("模板名称")]
        public string TempletName { get; set; }
        [XmlElement("模板GUID")]
        public Guid TempletGuid { get; set; }
        [XmlElement("模板类型")]
        public string TempletType { get; set; }
        [XmlElement("模板语言")]
        public string TempletLanguage { get; set; }
        [XmlArray("模板行集"), XmlArrayItem("模板行")]
        public List<PrintTempletRowV2d0> PrintTempletRowList { get; set; }
        public PrintTempletV2d0()
        {
            TempletName = "TMU220模板";
            TempletType = "normal";
            TempletGuid = Guid.Empty;
            TempletLanguage = "zh";
            PrintTempletRowList = new List<PrintTempletRowV2d0>();
        }
        public PrintTempletV2d0(string name,string type)
        {
            TempletName = name;
            TempletType = type;
            TempletGuid = Guid.NewGuid();
            TempletLanguage = "zh";
            PrintTempletRowList = new List<PrintTempletRowV2d0>();
        }
       
    }
    /// <summary>
    /// 打印模板行
    /// </summary>
    [XmlRoot("打印模板行")]
    [Serializable]
    public class PrintTempletRowV2d0
    {
        /// <summary>
        /// 行序号 
        /// </summary>
        [XmlElement("行序号")]
        public int RowIndex { get; set; }
        [XmlElement("模板Uid")]
        public int ParentId { get; set; }
        [XmlElement("行Guid")]
        public Guid RowGuid { get; set; }
        /// <summary>
        /// 打印模式 Text文本 Image图像
        /// </summary>
        [XmlElement("打印模式")]
        public string PrintMode { get; set; }
        /// <summary>
        /// 所属打印区域
        /// </summary>
        [XmlElement("区域类型")]
        public AreaTypeV2d0 AreaType { get; set; }
        /// <summary>
        /// 模板要素列表
        /// </summary>
        [XmlArray("模板元素集"), XmlArrayItem("模板元素")]
        public List<PrintTempletElementV2d0> TempletElementList { get; set; }
        public PrintTempletRowV2d0()
        {
            ParentId = 0;
            RowIndex = 0;
            PrintMode = "Text";
            AreaType = AreaTypeV2d0.Header;
            TempletElementList = new List<PrintTempletElementV2d0>();
        }
        /// <summary>
        /// 初始化包含左中右三个行元素
        /// </summary>
        public void Init3Element()
        {
            PrintTempletElementV2d0 templetElementLeft = new PrintTempletElementV2d0();
            templetElementLeft.HorizontalAlign = "Left";
            PrintTempletElementV2d0 templetElementCenter = new PrintTempletElementV2d0();
            templetElementCenter.HorizontalAlign = "Center";
            PrintTempletElementV2d0 templetElementRight = new PrintTempletElementV2d0();
            templetElementRight.HorizontalAlign = "Right";
            TempletElementList.Add(templetElementLeft);
            TempletElementList.Add(templetElementCenter);
            TempletElementList.Add(templetElementRight);
        }
    }
    /// <summary>
    /// 打印模板元素类
    /// </summary>
    [XmlRoot("模板元素")]
    [Serializable]
    public class PrintTempletElementV2d0
    {
        [XmlElement("元素序号")]
        public int ElementIndex { get; set; }
        /// <summary>
        /// 内容文本，如果包含"::"为类::属性，否则就是原样打印文本
        /// </summary>
        [XmlElement("内容文本")]
        public string ContentText { get; set; }
        /// <summary>
        /// 垂直对齐方式 Top,Middle，Bottom 图像打印模式
        /// </summary>
        [XmlElement("垂直对齐方式")]
        public string VerticalAlign { get; set; }
        //public ContentAlignment aa= ContentAlignment.
        /// <summary>
        /// 水平对齐方式，Left，Center，Right
        /// </summary>
        [XmlElement("水平对齐方式")]
        public string HorizontalAlign { get; set; }
        /// <summary>
        /// 字体名称 图像打印模式
        /// </summary>
        [XmlElement("字体名称")]
        public string FontName { get; set; }
        /// <summary>
        /// 字体颜色 Red,Black
        /// </summary>
        [XmlElement("字体颜色")]
        public string FontColor { get; set; }
        /// <summary>
        /// 字号
        /// </summary>
        [XmlElement("字号")]
        public int FontSize { get; set; }
        /// <summary>
        /// 是否倍宽 文本打印模式
        /// </summary>
        [XmlElement("是否倍宽")]
        public bool IsDoubleWidth { get; set; }
        /// <summary>
        /// 是否倍高 文本打印模式
        /// </summary>
        [XmlElement("是否倍高")]
        public bool IsDoubleHeight { get; set; }
        /// <summary>
        /// 是否下划线 
        /// </summary>
        [XmlElement("下划线")]
        public bool IsUnderLine { get; set; }
        /// <summary>
        /// 是否粗体 
        /// </summary>
        [XmlElement("粗体")]
        public bool IsBold { get; set; }
        /// <summary>
        /// 打印模式 Text,Image
        /// </summary>
        [XmlElement("打印模式")]
        public string PrintMode { get; set; }
        [XmlElement("是否为分割线-")]
        public bool IsCutoffLine { get; set; }
        public PrintTempletElementV2d0()
        {
            ContentText = "模板元素";
            VerticalAlign = "Bottom";
            HorizontalAlign = "Left";
            FontName = "宋体";
            FontSize = 10;
            FontColor = "Black";
            IsDoubleHeight = false;
            IsDoubleWidth = false;
            IsUnderLine = false;
            IsBold = false;
            PrintMode = "Text";
            IsCutoffLine = false;
        }
    }
    /// <summary>
    /// 模板行所属打印区域，小票头，小票明细，小票脚
    /// </summary>
    public enum AreaTypeV2d0
    {
        Header,
        Detail,
        Footer
    }
}
